package cn.message.web;

import cn.itcast.mapper.horsemanMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.util.OutputStringController;
import cn.message.service.IHorsemanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-03-25 16:26
 */
@Slf4j
@RestController
@RequestMapping(value="/dhn/message",produces="text/html;charset=UTF-8")
public class HorsemanController {

    @Resource(name = "horsemanService")
    private IHorsemanService horsemanService;

    @Resource
    private horsemanMapper horsemanMapper;

    @Resource
    OutputStringController osc;

    /**
     * 返回所有骑手列表信息
     * @return
     */
    @PostMapping("/getAllHorseman")
    @ResponseBody
    public String getAllHorseman(@RequestBody Map<String,String> map, horseman horseman){
        //获取前台提交的数据
        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));

        String results = "";
        Map<String, Object> resultMap = horsemanService.getAllHorseMan(page, limit, horseman);
        //获取所有骑手列表信息
        Object horsemanList = resultMap.get("horsemanList");
        //获取符合查询条件的总记录条数
        int count = (int)resultMap.get("count");

        if(horsemanList == null){
            results = osc.resultFailure("no", "");
            log.debug("查询所有骑手信息失败");
        }
        else {
            results = osc.resultSuccess("yes",horsemanList, count);
            log.debug("查询所有骑手信息成功");
        }

        return results;
    }

    /**
     *
     * @Description:
     * 获取所有用户
     * @auther: 欧阳辉
     * @date: 15:27 2020/3/31
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/getAllUser")
    @ResponseBody
    public String getAllUser(@RequestBody Map<String,String> map){
        //获取前台提交的数据
//        Integer page = Integer.parseInt(map.get("page"));
//        Integer limit = Integer.parseInt(map.get("limit"));
//        Byte gender = Byte.parseByte(map.get("gender"));
//        String userName = map.get("userName");
//        String nickName = map.get("nickName");

        String results = "";

        Map<String, Object> resultMap = horsemanService.getAllUser(map);
        //获取所有骑手列表信息
        Object userList = resultMap.get("userList");
        //获取符合查询条件的总记录条数
        int count = (int)resultMap.get("count");

        if(userList == null){
            results = osc.resultFailure("no", "");
            log.debug("查询所有骑手信息失败");
        }
        else {
            results = osc.resultSuccess("yes",userList, count);
            log.debug("查询所有骑手信息成功");
        }

        return results;
    }

    /**
     * 根据骑手id删除骑手
     * @param map
     * @return
     */
    @RequestMapping("/deleteHorsemanByHorsemanId")
    @ResponseBody
    public String deleteHorsemanByHorsemanId(@RequestBody Map<String,Object> map){
        String results = "";
        int horsemanId = (int)map.get("horsemanId");
        Boolean aBoolean = horsemanService.deleteHorsemanByHorsemanId(horsemanId);
        if(aBoolean = false){
            results = osc.resultFailure("系统异常，删除失败", "");
        }
        else {
            results = osc.success("yes");
        }
        return results;
    }


    /**
     * 增加骑手
     * @param map
     * @return
     */
    @RequestMapping("/addHorseman")
    @ResponseBody
    public String addHorseman(@RequestBody Map<String,Object> map){
        String results="";
        horseman horseman = new horseman();
        if (map.get("horsemanNumber") == null || map.get("horsemanNumber").equals("")) {
            return osc.failure("增加骑手失败，用户名不能为空");
        }
        //判断输入的用户名是否已存在
        Boolean exist = horsemanService.isExist((String) map.get("horsemanNumber"));
        if(exist){
            return osc.failure("该用户名已存在");
        }

        if (map.get("horsemanPassword") == null || map.get("horsemanPassword").equals("")) {
            return osc.failure("增加骑手失败，密码不能为空");
        }
        if (map.get("horsemanArea") == null || map.get("horsemanArea").equals("")) {
            return osc.failure("增加骑手失败，区域不能为空");
        }
        if (map.get("horsemanName") == null || map.get("horsemanName").equals("")) {
            return osc.failure("增加骑手失败，骑手姓名不能为空");
        }
        if (map.get("horsemanStarLevel") == null || map.get("horsemanStarLevel").equals("")) {
            return osc.failure("增加骑手失败，骑手星级不能为空");
        }

        horseman.setHorsemanNumber((String)map.get("horsemanNumber"));
        horseman.setHorsemanPassword((String)map.get("horsemanPassword"));
        horseman.setHorsemanArea((String)map.get("horsemanArea"));
        horseman.setHorsemanName((String)map.get("horsemanName"));
        horseman.setHorsemanPhone((String)map.get("horsemanPhone"));
        horseman.setHorsemanStarLevel((String)map.get("horsemanStarLevel"));
        horseman.setHorsemanIsopen(0+"");
        horseman.setHorsemanType("1");
        boolean aBoolean = horsemanService.addHorseman(horseman);
        if(!aBoolean){
            results = osc.failure("系统异常，增加骑手失败");
        }
        else {
            results = osc.success("增加骑手成功");
        }

        return results;
    }



    /**
     * 修改骑手信息
     * @param map
     * @return
     */
    @RequestMapping("/updateHorseman")
    public String updateHorseman(@RequestBody Map<String,Object> map){

        String results = "";
        horseman horseman;
        if(map.get("horsemanNumber") == null || map.get("horsemanNumber").equals("")){
            results = osc.failure("用户名或密码不能为空");
        }
        else if(map.get("horsemanPassword") == null || map.get("horsemanPassword").equals("")){
            results = osc.failure("用户名或密码不能为空");
        }
        else if(map.get("horsemanId") == null || map.get("horsemanId").equals("")){
            results = osc.failure("未获取到用户的id");
        }
        else if (map.get("horsemanName") == null || map.get("horsemanName").equals("")) {
            results = osc.failure("增加骑手失败，骑手姓名不能为空");
        }
        else if (map.get("horsemanStarLevel") == null || map.get("horsemanStarLevel").equals("")) {
            results = osc.failure("增加骑手失败，骑手星级不能为空");
        }
        else if (map.get("horsemanType") == null || map.get("horsemanType").equals("")){
            results = osc.failure("增加骑手失败,骑手类型不能为空");
        }


        else {
            horseman = new horseman();
            horseman.setHorsemanNumber((String)map.get("horsemanNumber"));
            horseman.setHorsemanPassword((String)map.get("horsemanPassword"));
            horseman.setHorsemanPhone((String)map.get("horsemanPhone"));
            horseman.setHorsemanId((Integer) map.get("horsemanId"));
            horseman.setHorsemanArea((String)map.get("horsemanArea"));
            horseman.setHorsemanName((String)map.get("horsemanName"));
            horseman.setHorsemanStarLevel((String)map.get("horsemanStarLevel"));
            horseman.setHorsemanType((String)map.get("horsemanType"));
            Boolean aBoolean = horsemanService.updateHorseman(horseman);
            if(aBoolean == false){
                results = osc.failure("系统异常，修改失败");
            }
            else {
                results = osc.success("修改成功");
            }
        }
        return results;
    }

    /**
     * 开通和关闭骑手
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/openAndCloseHorseman")
    public String openAndCloseHorseman(@RequestBody Map<String,String> map){

        String results = "";
        horseman horseman;
        horseman = new horseman();
        horseman.setHorsemanId(Integer.parseInt(map.get("horsemanId")));
        horseman.setHorsemanIsopen((String)map.get("horsemanIsopen"));
        Boolean aBoolean = horsemanService.updateHorseman(horseman);
        String msg = "";
        if(aBoolean == false){
            if(Integer.parseInt(map.get("horsemanIsopen")) == 1){
                msg = "系统异常，开通失败";
            }else {
                msg = "系统异常，关闭失败";
            }
            results = osc.failure(msg);
        }
        else {
            if(Integer.parseInt(map.get("horsemanIsopen")) == 1){
                msg = "开通成功";
            }else {
                msg = "关闭成功";
            }
            results = osc.success(msg);
        }

        return results;
    }

}
